package com.cultusoft.groovin.plugins.tomcat;

import com.cultusoft.groovin.utils.SystemProperties;
import com.cultusoft.groovin.log.GroovinLogger
import com.cultusoft.groovin.configuration.Configuration

class TomcatServerStarter {

    def logger = new GroovinLogger(this)
	
 	public void run() {
 		logger.log "Starting Tomcat"
 		startTomcatServer()
		waitForTomcatToStart()
 		logger.log("Tomcat has started")
 	}
 	
 	def waitForTomcatToStart() {
 		new TomcatStartupVerifier().run()	
 	}
 	
 	private void startTomcatServer() {
		def systemProperties = new SystemProperties()
 		
 		if (systemProperties.isLinux()) {
			startTomcatServerInLinux(); 			
 		} else {
 			startTomcatServerInWindows();
 		} 		
 	}
 	
 	private void startTomcatServerInWindows() {
 		def process = ("cmd /c " + System.getenv("CATALINA_HOME") + "\\bin\\startup.bat").execute()
 		process.in.eachLine { line -> logger.log(line) }
 	}
 	
 	private void startTomcatServerInLinux() {
 		def process = (System.getenv("CATALINA_HOME") + "/bin/startup.sh").execute()
 		process.in.eachLine { line -> logger.log(line) }
 	}

}
